package org.ws.task.schedule.dispatcher;

import com.google.common.collect.Maps;
import org.ws.task.model.TaskConfig;

import java.util.Map;

public class DispatcherFactory {
    private static final DispatcherFactory instance = new DispatcherFactory();

    private final Map<TaskConfig.DispatchStrategy, Dispatcher> itemScheduleMap;
    private final AllNodeDispatcher allNodeDispatcher;

    private DispatcherFactory(){
        this.itemScheduleMap = Maps.newConcurrentMap();
        register(new HashDispatcher());
        register(new PollDispatcher());
        register(new RandomDispatcher());
        allNodeDispatcher = new AllNodeDispatcher();
    }

    public static DispatcherFactory getInstance(){
        return instance;
    }

    private void register(Dispatcher dispatcher){
        this.itemScheduleMap.put(dispatcher.strategy(),dispatcher);
    }

    public Dispatcher getDispatcher(TaskConfig.DispatchStrategy taskStrategy,TaskConfig.SendModel sendModel){
        if(sendModel == TaskConfig.SendModel.CLUSTERING) {
            return this.itemScheduleMap.get(taskStrategy);
        }
        else {
            return this.allNodeDispatcher;
        }
    }
}
